package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.canteens.dao.SupplierFoodDao;
import io.renren.modules.canteens.dto.FoodDTO;
import io.renren.modules.canteens.dto.SupplierDTO;
import io.renren.modules.canteens.dto.SupplierFoodDTO;
import io.renren.modules.canteens.entity.FoodEntity;
import io.renren.modules.canteens.entity.SupplierFoodEntity;
import io.renren.modules.canteens.service.FoodService;
import io.renren.modules.canteens.service.SupplierFoodService;
import io.renren.modules.canteens.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 供应商_食材表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class SupplierFoodServiceImpl extends CrudServiceImpl<SupplierFoodDao, SupplierFoodEntity, SupplierFoodDTO> implements SupplierFoodService {

    @Autowired
    private SupplierFoodDao supplierFoodDao;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private FoodService foodService;
    @Override
    public QueryWrapper<SupplierFoodEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SupplierFoodEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public SupplierFoodDTO getSupplierFood(Long supplierId, Long foodId) {
        SupplierFoodDTO supplierFood = supplierFoodDao.getSupplierFood(supplierId, foodId);
        return supplierFood;
    }

    @Override
    public PageData<SupplierFoodDTO> getName(Map<String, Object> params) {
        String supplierName = (String)params.get("supplierName");
        QueryWrapper<SupplierFoodEntity> wrapper = new QueryWrapper<>();
        /*String foodName = (String)params.get("foodName");
        if(null!=foodName ||""!=foodName){
            List<FoodDTO> likeName = foodService.getLikeName(foodName);
            if(likeName.size()>0){
                for (FoodDTO foodDTO:likeName) {
                    Long foodId = foodDTO.getFoodId();
                    wrapper.like( "food_id", foodId);
                }
            }
        }*/
        if(null != supplierName){
            List<SupplierDTO> likeName = supplierService.getLikeName(supplierName);
            if(null != likeName.get(0)){
                Long supplierId = likeName.get(0).getSupplierId();
                wrapper.like( "supplier_id", supplierId);
            }
        }
        IPage<SupplierFoodEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                wrapper);
        PageData<SupplierFoodDTO> pageData = getPageData(page, SupplierFoodDTO.class);
        return pageData;
    }
}